package com.system.business.controller;

import com.system.business.req.ConfirmOrderDoReq;
import com.system.business.req.ConfirmOrderQueryReq;
import com.system.business.req.ConfirmOrderSaveReq;
import com.system.business.resp.ConfirmOrderQueryResp;
import com.system.business.service.ConfirmOrderService;
import com.system.common.resp.CommonResp;
import com.system.common.resp.PageResp;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

@RestController
@RequestMapping("/confirm-order")
public class ConfirmOrderAdminController {
    private static final Logger LOG = LoggerFactory.getLogger(ConfirmOrderAdminController.class);

    @Autowired
    private StringRedisTemplate redisTemplate;
    @Resource
    private ConfirmOrderService confirmOrderService;
    @Value("${spring.profiles.active}")
    private String env;

    @PostMapping("/save")
    public CommonResp<Object> save(@Valid @RequestBody ConfirmOrderSaveReq req) {
        confirmOrderService.save(req);
        return new CommonResp<>();
    }

    @GetMapping("/query-list")
    public CommonResp<PageResp<ConfirmOrderQueryResp>> queryList(@Valid ConfirmOrderQueryReq req) {
        PageResp<ConfirmOrderQueryResp> list = confirmOrderService.queryList(req);
        return new CommonResp<>(list);
    }

    // 接口的资源名称不要和接口路径一致，会导致限流后走不到降级方法中
    @PostMapping("/do")
    public CommonResp<Object> doConfirm(@Valid @RequestBody ConfirmOrderDoReq req) {
//        if (!env.equals("dev")) {
//            // 图形验证码校验
//            String imageCodeToken = req.getImageCodeToken();
//            String imageCode = req.getImageCode();
//            String imageCodeRedis = redisTemplate.opsForValue().get(imageCodeToken);
//            LOG.info("从redis中获取到的验证码：{}", imageCodeRedis);
//            if (ObjectUtils.isEmpty(imageCodeRedis)) {
//                return new CommonResp<>(false, "验证码已过期", null);
//            }
//            // 验证码校验，大小写忽略，提升体验，比如Oo Vv Ww容易混
//            if (!imageCodeRedis.equalsIgnoreCase(imageCode)) {
//                return new CommonResp<>(false, "验证码不正确", null);
//            } else {
//                // 验证通过后，移除验证码
//                redisTemplate.delete(imageCodeToken);
//            }
//        }
//
//        //Long id = beforeConfirmOrderService.beforeDoConfirm(req);
//        return new CommonResp<>(String.valueOf(id));
        confirmOrderService.doConfirm(req);
        return new CommonResp<>();
    }
//
//    @GetMapping("/query-line-count/{id}")
//    public CommonResp<Integer> queryLineCount(@PathVariable Long id) {
//        /Integer count = confirmOrderService.queryLineCount(id);
//        return new CommonResp<>(count);
//    }
}
